﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System;
using System.Security.Cryptography;
using XLua;
using UnityEngine.Networking;
using UnityEngine.EventSystems;
[LuaCallCSharp]
public static class UtilsCSharp
{

    public static string GetDataPath
    {

        get
        {
            if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            {
                return "jar:file://" + Application.dataPath + "!/assets/";
            }
#if UNITY_EDITOR
            return Application.streamingAssetsPath + "/";
#elif UNITY_IOS
        return Application.streamingAssetsPath + "/";
#else
        return Application.streamingAssetsPath + "/";
#endif
        }
    }
    public static void AddOnClick(GameObject go, LuaFunction luafuc)
    {
        UIEventListener.Get(go).onDown += delegate (GameObject o)
        {
            if (luafuc != null)
            {
                luafuc.Call(o);
            }
        };

    }

    public static void RemoveOnClick(GameObject go)
    {
        UIEventListener.Get(go).onClick = null;
    }


    public static void AddOnDrag(GameObject go, LuaFunction luafuc)
    {
        UIEventListener.Get(go).onDrag += delegate (PointerEventData o)
        {
            if (luafuc != null)
            {
                luafuc.Call(o);
            }
        };
    }


    public static void AddOnCollider(GameObject go, LuaFunction luafuc)
    {
        ColliderTools.Get(go).onCollisionEnter += delegate (Collision o)
        {
            if (luafuc != null)
            {
                luafuc.Call(o);
            }
        };
    }




    public static void AddButtonOnClick(GameObject go, LuaFunction luafuc)
    {

        Button button = go.GetComponent<Button>();
        if (button != null)
        {
            button.onClick.RemoveAllListeners();
        }
        else
        {
            button = go.AddComponent<Button>();
        }
        button.onClick.AddListener(delegate ()
        {
            luafuc.Call(go);
        });
    }

    public static void RemoveButtonOnClick(GameObject go)
    {

        Button button = go.GetComponent<Button>();
        if (button != null)
        {
            button.onClick.RemoveAllListeners();
        }


    }
    public static UnityEngine.Object LoadAssetObj(string assetName)
    {
        if (string.IsNullOrEmpty(assetName)) return null;
        Debug.Log(assetName);
        // return AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(assetName);   
        return Resources.Load<UnityEngine.Object>(assetName);

    }

    public static Sprite LoadSprite(string spriteName)
    {
        if (string.IsNullOrEmpty(spriteName)) return null;
        Debug.Log(spriteName);
        return Resources.Load<Sprite>(spriteName);
    }



    public static string MD5(string source)
    {
        Debug.Log(source);
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.UTF8.GetBytes(source);
        byte[] md5Data = md5.ComputeHash(data, 0, data.Length);
        md5.Clear();

        string destString = "";
        Debug.Log(destString);
        for (int i = 0; i < md5Data.Length; i++)
        {
            destString += md5Data[i].ToString("x2");
        }

        return destString;
    }

    public static void Log(string log)
    {
        Debug.Log(log);
    }

    public static void LogError(string log)
    {
        Debug.LogError(log);
    }
    //射线
    public static bool RayFunction(Ray ray, out RaycastHit hit, int length, int layer)
    {
        return Physics.Raycast(ray, out hit, length, 1 << layer);
    }

    static IEnumerator WebRequestCB(string url, LuaFunction CB)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {
            yield return www.Send();
            if (www.isNetworkError)
            {
                Debug.LogError(www.isNetworkError);
            }
            else
            {
                if (CB != null)
                {
                    CB.Call(www.downloadHandler.data);
                }
            }
        }
    }

    public static IEnumerator LoadSceneAsync(string sceneName)
    {
        int toProgress = 0;
        int displayProgress = 0;
        AsyncOperation async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName);
        async.allowSceneActivation = false;
        while (async.progress < 0.9)
        {
            toProgress = (int)(async.progress * 100);

            while (displayProgress <= toProgress)
            {
                displayProgress++;

                yield return new WaitForEndOfFrame();
            }
        }
        toProgress = 100;

        while (displayProgress < toProgress)
        {
            displayProgress++;

            yield return new WaitForEndOfFrame();

        }
        async.allowSceneActivation = true;

    }

    public static GameObject LoadAssetData_Game(string path)
    {
        GameObject load = new GameObject();

#if UNITY_EDITOR
        AssetBundle ab = AssetBundle.LoadFromFile(GetDataPath + "AB/" + path + ".main");
        load = ab.LoadAsset<GameObject>(path);
        ab.Unload(false);
#elif UNITY_ANDROID
        AssetBundle ab = AssetBundle.LoadFromFile(GetDataPath +"AB/"+path+ ".main");
        load = ab.LoadAsset<GameObject>(path);

        ab.Unload(false);
#endif
        return load;


    }

    public static Sprite LoadAssetData_PNG(string path)
    {

        Sprite load = null;

#if UNITY_EDITOR
        AssetBundle ab = AssetBundle.LoadFromFile(GetDataPath + "AB/" + path + ".main");
        load = ab.LoadAsset<Sprite>(path);
        ab.Unload(false);
#elif UNITY_ANDROID
        AssetBundle ab = AssetBundle.LoadFromFile(GetDataPath +"AB/"+path+ ".main");
        load = ab.LoadAsset<Sprite>(path);
        ab.Unload(false);
#elif UNITY_IOS

#elif UNITY_STANDALONE_WIN

#endif

        return load;
    }
}
